<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Santris extends CI_Controller {

    var $santri_id;
    var $status;
    var $cari;
    var $message;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->auth->restrict();

        $this->load->helper('text');
        $this->load->model('m_santri');
        $uri = $this->uri->uri_to_assoc(3);

        if (isset($uri['id'])) {
            $this->santri_id = $uri['id'];
        }

        if (isset($uri['status'])) {
            $this->status = $uri['status'];
        }

        if (isset($_POST['in_cari'])) {
            $this->cari = $this->input->post('in_cari');
            $this->session->set_userdata('cari', $this->cari);
        }

        if ($this->session->userdata('cari')) {
            $this->cari = $this->session->userdata('cari');
        }

        if ($this->auth->cek_role_member() != '1') {
            redirect('homes');
        }
    }

    public function index($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_santri->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/santris/index/page/';
        $config['total_rows'] = $this->m_santri->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_santri->num_pages = $config['per_page'];
        $this->m_santri->offset = $this->uri->segment(4);
        $this->m_santri->paginate = TRUE;
        $this->pagination->initialize($config);

        $santris = $this->m_santri->get();
        $data['title'] = $this->lang->line('menu_santri') . ' ' . $this->lang->line('webname');
        $data['santris'] = $santris;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('santri_view', $data);
        $this->load->view('view_footer', $data);
    }

    public function view() {
        $this->session->unset_userdata('cari');

        redirect('santris/');
    }

    public function add($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_nama', $this->lang->line('nama'), 'trim|required|xss_clean|callback__santris_name_check');
        $this->form_validation->set_rules('in_kelamin', $this->lang->line('kelamin'), 'trim|required');
        $this->form_validation->set_rules('in_sekolah', $this->lang->line('sekolah'), 'trim|required');
        $this->form_validation->set_rules('in_status', $this->lang->line('status'), 'trim|required');
        //$this->form_validation->set_rules('in_tahun_masuk', $this->lang->line('tahun_masuk'), 'trim|required|max_length[4]');

        if ($this->form_validation->run() == FALSE) {
            $santris_kelamin = $this->auth->load_kelamin();
            $aktifs = $this->auth->load_status_santri();
            $sekolahs = $this->auth->load_sekolah_santri();

            $data['title'] = $this->lang->line('menu_santri') . ' ' . $this->lang->line('webname');
            $data['santri_id'] = $this->santri_id;
            $data['aktifs'] = $aktifs;
            $data['santris_kelamin'] = $santris_kelamin;
            $data['sekolahs'] = $sekolahs;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('santri_form', $data);
            $this->load->view('view_footer', $data);
        } else {
            $this->m_santri->santri_nama = $this->input->post('in_nama');
            $this->m_santri->santri_kelamin = $this->input->post('in_kelamin');
            $this->m_santri->santri_tempat_lahir = $this->input->post('in_tempat_lahir');
            $this->m_santri->santri_tanggal_lahir = date('Y-m-d', strtotime($this->input->post('in_tanggal_lahir')));
            $this->m_santri->santri_nama_ayah = $this->input->post('in_nama_ortu');
            $this->m_santri->santri_sekolah = $this->input->post('in_sekolah');
            $this->m_santri->santri_asal_sekolah = $this->input->post('in_asal_sekolah');
            $this->m_santri->santri_alamat = $this->input->post('in_alamat');
            //$this->m_santri->santri_tahun = $this->input->post('in_tahun_masuk');
            $this->m_santri->santri_status = $this->input->post('in_status');
            $this->m_santri->santri_tanggal = date('Y-m-d', strtotime($this->input->post('in_tanggal'))); //date('Y-m-d H:i:s');

            $this->m_santri->santri_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_santri->santri_dirubah = date('Y-m-d H:i:s');

            $this->m_santri->santri_id = '';
            $this->m_santri->save();

            $this->view();
            //redirect('santris/');
        }
    }

    public function edit() {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_nama', $this->lang->line('nama'), 'trim|required|xss_clean');
        $this->form_validation->set_rules('in_kelamin', $this->lang->line('kelamin'), 'trim|required');
        $this->form_validation->set_rules('in_sekolah', $this->lang->line('sekolah'), 'trim|required');
        $this->form_validation->set_rules('in_status', $this->lang->line('status'), 'trim|required');
        // $this->form_validation->set_rules('in_tahun_masuk', $this->lang->line('tahun_masuk'), 'trim|required|max_length[4]');


        if ($this->form_validation->run() == FALSE) {
            $santris_kelamin = $this->auth->load_kelamin();
            $aktifs = $this->auth->load_status_santri();
            $sekolahs = $this->auth->load_sekolah_santri();

            $data['title'] = $this->lang->line('menu_santri') . ' ' . $this->lang->line('webname');
            $data['santri_id'] = $this->santri_id;
            $data['aktifs'] = $aktifs;
            $data['santris_kelamin'] = $santris_kelamin;
            $data['sekolahs'] = $sekolahs;
            $data['message'] = $this->message;
            $data['santris'] = $this->m_santri->get_by_id($this->santri_id);

            $this->load->view('view_header', $data);
            $this->load->view('santri_form', $data);
            $this->load->view('view_footer', $data);
        } else {
            $this->m_santri->santri_nama = $this->input->post('in_nama');
            $this->m_santri->santri_kelamin = $this->input->post('in_kelamin');
            $this->m_santri->santri_tempat_lahir = $this->input->post('in_tempat_lahir');
            $this->m_santri->santri_tanggal_lahir = date('Y-m-d', strtotime($this->input->post('in_tanggal_lahir')));
            $this->m_santri->santri_nama_ayah = $this->input->post('in_nama_ortu');
            $this->m_santri->santri_sekolah = $this->input->post('in_sekolah');
            $this->m_santri->santri_asal_sekolah = $this->input->post('in_asal_sekolah');
            $this->m_santri->santri_alamat = $this->input->post('in_alamat');
            //$this->m_santri->santri_tahun = $this->input->post('in_tahun_masuk');
            $this->m_santri->santri_status = $this->input->post('in_status');
            //$this->m_santri->santri_tanggal = date('Y-m-d H:i:s');
            $this->m_santri->santri_tanggal = date('Y-m-d', strtotime($this->input->post('in_tanggal')));

            $this->m_santri->santri_oleh = $this->auth->get_member_by_idnya($this->session->userdata('member_id'));
            $this->m_santri->santri_dirubah = date('Y-m-d H:i:s');

            $this->m_santri->santri_id = $this->santri_id;
            $this->m_santri->save();
            //echo 'kelamin ' . $this->m_santri->santri_kelamin;
            $this->view();
        }
    }

    public function delete() {
        $this->m_santri->santri_id = $this->santri_id;
        $this->m_santri->delete();

        $this->view();
    }

    public function _santris_name_check($str) {
        if ($this->m_santri->get_santri_name($str)) {
            $this->form_validation->set_message('_santris_name_check', 'Nama pengguna " ' . $str . ' " sudah digunakan.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

/* End of file santris.php */
/* Location: ./application/controllers/santris.php */